"use strict";
const BaseController = require("../base");

// 定义创建接口的请求参数规则
const getCategoryRule = {
  categoryId: "string",
};

const saveCategoryRule = {
  name: "string",
  parentId: "string",
};
const saveCategory1Rule = {
  name: "array",
  parentId: "string",
};

class CategoriesController extends BaseController {
  async getCategory1() {
    const ctx = this.ctx;
    const data = await ctx.service.product.categories.getCategory1();
    this.success(data);
  }
  async getCategory2() {
    const ctx = this.ctx;
    // 校验参数是否符合我们预期的格式
    const category1Id = ctx.params.category1Id;
    // 如果参数校验未通过，将会抛出一个 status = 422 的异常
    ctx.validate(getCategoryRule, {
      categoryId: category1Id,
    });
    const data = await ctx.service.product.categories.getCategory2(category1Id);
    this.success(data);
  }
  async getCategory3() {
    const ctx = this.ctx;
    // 校验参数是否符合我们预期的格式
    const category2Id = ctx.params.category2Id;
    // 如果参数校验未通过，将会抛出一个 status = 422 的异常
    ctx.validate(getCategoryRule, {
      categoryId: category2Id,
    });
    const data = await ctx.service.product.categories.getCategory3(category2Id);
    this.success(data);
  }
  async saveCategory1() {
    const ctx = this.ctx;
    const { name } = ctx.request.body;
    const body = {
      name,
      parentId: "0",
    };
    ctx.validate(saveCategory1Rule, body);

    const data = await ctx.service.product.categories.saveCategory1(body);

    this.success(data);
  }
  async saveCategory2() {
    const ctx = this.ctx;
    const { name, category1Id } = ctx.request.body;
    const body = {
      name,
      parentId: category1Id,
    };

    ctx.validate(saveCategoryRule, body);

    const data = await ctx.service.product.categories.saveCategory2(body);
    // 设置响应体和状态码

    this.success(data);
    // ctx.status = 200;
  }
  async saveCategory3() {
    const ctx = this.ctx;
    const { name, category2Id } = ctx.request.body;
    const body = {
      name,
      parentId: category2Id,
    };
    ctx.validate(saveCategoryRule, body);

    const data = await ctx.service.product.categories.saveCategory3(body);

    this.success(data);
  }
}

module.exports = CategoriesController;
